var UserNO=localStorage.getItem("UserNO");
var currentwindow;
var counts = "";

window.onload = function() {
    mui.plusReady(function() {
        	WarehouseList();
			TakeTypeList();
		  document.getElementById('txtStockTakeNO').focus();     
		   $("#all").html("");
		   $("#all").html(
		   '<a style="font-size:100px; color:rgba(242,125,72,1.00); display:block; text-align:center;" class="mui-icon mui-icon-search"></a><a class="mui-title" style="color:rgba(128,128,128,1.00); position: initial; margin: 0 auto; height:' +
		   (window.screen.height - 200) + 'px">No data available at the moment</a>');
		   if (window.plus && plus.nativeUI) {
		   plus.nativeUI.closeWaiting();
		     }
		   
    });  // Close mui.plusReady
};  // Close window.onload
function showinfo(e) {
	e.preventDefault();
	change2(1);
}
function change2(e) {
	pageno = e;
	var pageall;
	var StockTakeNO = $("#txtStockTakeNO").val();
	var TakeType = $("#sltTakeType").val();
	var WarehouseNO = $("#sltWarehouse").val();
	var json={ 
		"PageIndex": parseInt(pageno),  // Use current page number
	    "PageSize": 10,
	    "StocktakeNO": StockTakeNO,
	    "WarehouseNO": WarehouseNO,
	    "TakeType": TakeType};
		
		
		
	xccFormPostAsync("api/PDA/GetStockTakeOrderList",  json, function(data) {
	      // Debug: Check response
	    
	    // Calculate total pages (ensure data.TotalCount exists server-side)
	    pageall =data.TotalPage || 1;  // Fallback to 1 if TotalCount missing
	    
	    if (data.Items && data.Items.length > 0) {
	        $("#all").html("");  // Clear existing content
	        var res = "";
	        
	        for (var i = 0; i < data.Items.length; i++) {
	            res += `
	                <li class="mui-table-view-cell" onclick="openPosition('${data.Items[i].StocktakeNO}')">
	                    <div class="list-title">StockTakeNO: ${data.Items[i].StocktakeNO}</div>
	                    <div class="list-detail">WarehouseNO: ${data.Items[i].WarehouseNO}</div>
	                    <div class="list-detail">TakeType: ${data.Items[i].TakeType}</div>
	                    <div class="list-detail">TakeDate: ${data.Items[i].TakeDate}</div>
	                </li>`;
	        }
	        
	        $("#all").append(res);  // Add all items at once
	
	        // Pagination controls
	        var pageContent = `Page <span id="a2">${pageno}</span>/<span id="a1">${pageall}</span>`;
	        if (pageno > 1) pageContent += '<a href="#" onclick="change2(--pageno)">Previous</a>';
	        if (pageno < pageall) pageContent += '<a href="#" onclick="change2(++pageno)">Next</a>';
	        
	        $("#page").html(pageContent);
	    } else {
	        $("#all").html(`
	            <a style="font-size:100px; color:rgba(242,125,72,1.00); display:block; text-align:center;" 
	               class="mui-icon mui-icon-search"></a>
	            <a class="mui-title" 
	               style="color:rgba(128,128,128,1.00); position: initial; margin: 0 auto; height:${window.screen.height - 400}px">
	               No data available
	            </a>
	        `);
	    }
	    
	    plus.nativeUI.closeWaiting();
	}, function(err) {
	    console.error("Error:", err);
	    plus.nativeUI.closeWaiting();
	});
}
function TakeTypeList()
{
	//GetTakeType
	
	xccpostAsync("api/PDA/GetTakeType", {}, function(data) {
		document.activeElement.blur();
		console.log(JSON.stringify(data));
		if (data.Code === 200) {
			data.Items.forEach(item => {
				$("#sltTakeType").append(
					`<option value="${item.Name}">${item.Name}</option>`
				);
			});
			// 设置初始焦点
			
		} else {
			mui.toast("failed。" + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	}, function(error) {
		console.log("没有数据....");
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	});
}

function WarehouseList() {
	xccpostAsync("api/PDA/GetWarehouseInfo", {}, function(data) {
		document.activeElement.blur();
		console.log(JSON.stringify(data));
		if (data.Code === 200) {
			data.Items.forEach(item => {
				$("#sltWarehouse").append(
					`<option value="${item.WarehouseNO}">${item.WarehouseName}</option>`
				);
			});
			// 设置初始焦点
			
		} else {
			mui.toast("failed。" + data.Message, {
				duration: '1000',
				type: 'div'
			});
		}
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	}, function(error) {
		console.log("没有数据....");
		plus.nativeUI.closeWaiting(); // 关闭转圈圈特效
	});
}
function openPosition(StockTakeNO) {
	console.log(UserNO)
	mui.openWindow({
		url: 'WmsStocktakeOrderInfo_2.html',
		action: UserNO,
		extras: {
			StockTakeNO: StockTakeNO
		}
	});
}

